<?php 
defined('BASEPATH') OR exit('No direct script access allowed');
 function utf8_to_ascii($str){
	
		$chars = array(
			'a'	=>	array('ấ','ầ','ẩ','ẫ','ậ','Ấ','Ầ','Ẩ','Ẫ','Ậ','ắ','ằ','ẳ','ẵ','ặ','Ắ','Ằ','Ẳ','Ẵ','Ặ','á','à','ả','ã','ạ','â','ă','Á','À','Ả','Ã','Ạ','Â','Ă','A'),
			'b' =>  array("B"),			
			'c' =>  array("C"),			
			'd'	=>	array('đ','Đ','D'),
			'e' =>	array('ế','ề','ể','ễ','ệ','Ế','Ề','Ể','Ễ','Ệ','é','è','ẻ','ẽ','ẹ','ê','É','È','Ẻ','Ẽ','Ẹ','Ê','E'),
			'f' =>  array("F"),
			'g' =>  array("G"),
			'h' =>  array("H"),
			'i'	=>	array('í','ì','ỉ','ĩ','ị','Í','Ì','Ỉ','Ĩ','Ị','I'),
			'j' =>  array("J"),
			'k' =>  array("K"),
			'l' =>  array("L"),
			'm' =>  array("M"),
			'n' =>  array("N"),			
			'o'	=>	array('ố','ồ','ổ','ỗ','ộ','Ố','Ồ','Ổ','Ô','Ộ','ớ','ờ','ở','ỡ','ợ','Ớ','Ờ','Ở','Ỡ','Ợ','ó','ò','ỏ','õ','ọ','ô','ơ','Ó','Ò','Ỏ','Õ','Ọ','Ô','Ơ','O'),	
			'p' =>  array("P"),
			'q' =>  array("Q"),
			'r' =>  array("R"),
			's' =>  array("S"),
			't' =>  array("T"),
			'u'	=>	array('ứ','ừ','ử','ữ','ự','Ứ','Ừ','Ử','Ữ','Ự','ú','ù','ủ','ũ','ụ','ư','Ú','Ù','Ủ','Ũ','Ụ','Ư','U'),
			'x' =>  array("X"),
			'y'	=>	array('ý','ỳ','ỷ','ỹ','ỵ','Ý','Ỳ','Ỷ','Ỹ','Ỵ','Y'),
			'v' =>  array("V"),
			'w' =>  array("W"),	
			'z' =>  array("Z"),			
		);
		
		foreach ($chars as $key => $arr) 
			foreach ($arr as $val)
				$str = str_replace($val,$key,$str);
		//$str = preg_replace('/[^a-zA-Z0-9_-\s]/','',$str); 
		$str = str_replace("(","-",trim($str));
		$str = str_replace(")","",trim($str));
		$str = str_replace(" ","-",trim($str));
		$str = str_replace("---","-",trim($str));
		$str = str_replace("--","-",trim($str));
		return $str;
 }
  function trim_utf8($str_start){
		$chars = array(
			'a'	=>	array('ấ','ầ','ẩ','ẫ','ậ','Ấ','Ầ','Ẩ','Ẫ','Ậ','ắ','ằ','ẳ','ẵ','ặ','Ắ','Ằ','Ẳ','Ẵ','Ặ','á','à','ả','ã','ạ','â','ă','Á','À','Ả','Ã','Ạ','Â','Ă','A'),
			'b' =>  array("B"),
			'c' =>  array("C"),			
			'd'	=>	array('đ','Đ','D'),
			'e' =>	array('ế','ề','ể','ễ','ệ','Ế','Ề','Ể','Ễ','Ệ','é','è','ẻ','ẽ','ẹ','ê','É','È','Ẻ','Ẽ','Ẹ','Ê','E'),
			'f' =>  array("F"),
			'g' =>  array("G"),
			'h' =>  array("H"),
			'i'	=>	array('í','ì','ỉ','ĩ','ị','Í','Ì','Ỉ','Ĩ','Ị',"I"),
			'j' =>  array("J"),
			'k' =>  array("K"),						
			'l' =>  array("L"),
			'm' =>  array("M"),
			'n' =>  array("N"),			
			'o'	=>	array('ố','ồ','ổ','ỗ','ộ','Ố','Ồ','Ổ','Ô','Ộ','ớ','ờ','ở','ỡ','ợ','Ớ','Ờ','Ở','Ỡ','Ợ','ó','ò','ỏ','õ','ọ','ô','ơ','Ó','Ò','Ỏ','Õ','Ọ','Ô','Ơ','O'),			
			'p' =>  array("P"),
			'q' =>  array("Q"),
			'r' =>  array("R"),			
			's' =>  array("S"),
			't' =>  array("T"),
			'z' =>  array("Z"),
			'x' =>  array("X"),
			'v' =>  array("V"),			
			'u'	=>	array('ứ','ừ','ử','ữ','ự','Ứ','Ừ','Ử','Ữ','Ự','ú','ù','ủ','ũ','ụ','ư','Ú','Ù','Ủ','Ũ','Ụ','Ư','U'),
			'y'	=>	array('ý','ỳ','ỷ','ỹ','ỵ','Ý','Ỳ','Ỷ','Ỹ','Ỵ','Y'),
			'w' =>  array('W'),
			'z' =>  array('Z'),
		);
		foreach ($chars as $key => $arr) 
			foreach ($arr as $val)
				$str = str_replace($val,$key,$str_start);
		$str = str_replace(" ","",trim($str));
		if($str == $str_start)
			return "";
		return $str;
	}
 	function chuyensolama($str_start) 
	{
		$chars = array('I'	=>	array('1'),'II' 	=>	array('2'),'III'	=>	array('3'),'IV'	=>	array('4'),'V'		=>	array('5'),'VI'	=>	array('6'),'VII'	=>	array('7'),'VIII'	=>	array('8'),'IX'	=>	array('9'),'X'		=>	array('10'));
		foreach ($chars as $key => $arr) 
			foreach ($arr as $val)
				$str = str_replace($val,$key,$str_start);
		$str = str_replace(" ","",trim($str));
		if($str == $str_start)
			return ;
		return $str;
	}
	function _url_title($str, $separator = 'dash', $lowercase = FALSE)
	{
		if ($separator == 'dash')
		{
			$search		= '_';
			$replace	= '-';
		}
		else
		{
			$search		= '-';
			$replace	= '_';
		}

		$trans = array(
						'%20'					=> '-',
						'&\#\d+?;'				=> '',
						'&\S+?;'				=> '',
						'\s+'					=> $replace,
						'[^a-z0-9\-\._]'		=> '',
						$replace.'+'			=> $replace,
						$replace.'$'			=> $replace,
						'^'.$replace			=> $replace,
						'\.+$'					=> ''
					  );

		$str = strip_tags($str);

		foreach ($trans as $key => $val)
		{
			$str = preg_replace("#".$key."#i", $val, $str);
		}

		if ($lowercase === TRUE)
		{
			$str = strtolower($str);
		}
		
		return trim(stripslashes($str));
	}
	function _title_compare($title_compare){
		$utf8_to_ascii     = utf8_to_ascii(trim($title_compare));
		$title_compare_c   = $utf8_to_ascii;
		$replace  		   = str_replace("-","",utf8_to_ascii(trim($title_compare))); // load bo dau -
		$title_compare_c  .= ($replace == $title_compare_c)?"":"|".$replace;
		/* bo het dau ky tu dac biet khi co khoang trang */
		$symbol  = preg_replace('/([^\pL\-\ ]+)/u','',$utf8_to_ascii);
		$symbol  = str_replace("--","-",str_replace("---","-",$symbol));
		$title_compare_c  .= ($utf8_to_ascii == $symbol)?"":"|".$symbol;
		/* bo het dau ky tu dac biet khi khong co khoang trang  */
		$symbol  = preg_replace('([^a-zA-Z0-9])','',$replace);
		$title_compare_c  .= ($replace == $symbol)?"":"|".$symbol;	
		$chuyensolama  = chuyensolama(str_replace("-","",utf8_to_ascii(trim($title_compare))));
		$title_compare_c .= !empty($chuyensolama)?"|".$chuyensolama:"";
		return $title_compare_c;
	}
	function trim_text($input, $width=60) {
		if(empty($input)) return $input ;
		
		if (strlen($input) <= $width) {
			return $input;
		}
		
		$output = substr($input,0,$width);
		
		//normals words are seldom more than 30 chars
		$pos = 0 ;
		$found = false ;
		
		for($i = $width ; $i >= 0 ; $i--) {
			 if(ctype_space($output[$i])) {
				$found = true ;
				break ;
			 }
			 $pos++ ;
		}
		
		if($found && ($pos > 0)) {
			$output = substr($output,0,($width-$pos));
			$output = rtrim($output) ;
		}
		
		$output .= '...' ;
		return $output;
	}
?>